#ifndef _CHAR_MANANGER_H_
#define _CHAR_MANANGER_H_

#include "Common.h"
#include "Resource.h"
#include "ResourceManager.h"
#include "Singleton.h"
#include "String.h"
#include "Threading/ThreadStarter.h"
#include "Threading/QueueThreadPool.h"

class CharPtr;
class ScriptFile;


class  CharManager:public ThreadRunableBase, public ResourceManager,public Singleton<CharManager>
{
public:
	CharManager();
	~CharManager();

	String	getCharStringName(const uint32 & serial);
	uint32	getCharUid(uint32 sid,const String & name);
	
	ResourcePtr  create(const uint32 & serial,const NameValuePairList* params = 0);
	ResourceCreateOrRetrieveResult  \
		createOrRetrieve(const uint32 & serial,const NameValuePairList* params = 0);
	ResourcePtr  load(const uint32 & serial);
	ResourcePtr     reload(ResourceHandle handle);


	//user manager start
	void	Startup(const uint32 & gid, const uint32 & thread_min,const uint32 & thread_max = 0,bool isBattleServer = true);
	void 	Shutdown(void);

	//得到下等级需要的经验值
	size_t loadLevelExps(void);
	uint32 getNextLevelExps(const uint32 & currLevel);

	//根据查询，返回符合条件的记录 offset 为翻页起点 limitcount 为取回角色最大数量 query 为查询条件在 WHERE之后
	bool	getCharsList(std::list<CharPtr> * chars,const uint32 & offset,const uint32 & limitcount,const char * query,...);

	//根据查询条件返回一个查询结果数量 query 为在WHERE之后的SQL语句
	uint32	getDBCount(const char * query,...);

	//用于得到一个Character结构，用于向数据库追加新效果的记录
	Resource * newDumyChar(void);
	void	freeDumyChar(Resource * dumy);

	//根据模板向数据库插入新的记录，返回插入的记录 执行load载入
	ResourcePtr	loadCharDatabase(Resource * dumy);

	//根据模板向数据库插入新的纪录，返回插入的记录，不执行load操作
	ResourcePtr	createCharDatabase(Resource * dumy);

	ResourcePtr   newDBObject(void);
	ResourcePtr   getDBObjectByHandle(const uint32 & handle);
	bool	getDBObjects(std::list<ResourcePtr> * objs,const uint32 & offset,const uint32 & limitcount,const char * query,...);


	const bool & isBattleServer(void)	{	return m_battle_server;	}
	const uint32 & getGroupID(void)		{	return m_default_gid;	}
 
	ResourcePtr	CreateScript(const uint32 & index);
	ResourcePtr	LoadScript(const uint32 & index);
	ScriptFile    *  getScriptFile(void)	{	return m_scriptFile;	}

        //返回 section 为 "UINT32_LIST_%u" 的整形
        uint32  getScriptUInt32List(const uint32 & index);
        //返回 section 为 "INT32_LIST_%u" 的整形
        int32  getScriptInt32List(const uint32 & index);
        //返回 section 为 "FLOAT_LIST_%u" 的浮点
        float getScriptFloatList(const uint32 & index);
        //返?? section 为 "STRING_LIST_%u" 的字符
        String getScriptStringList(const uint32 & index);
	
	//virual from  ThreadRunableBase
        bool  run(void *p);
	bool    StartCharacterProcess(void);

	const uint32 getCurrentUpdateTime(void) {       return m_currentUpdateTime;     }

protected:
	uint32		m_default_gid;
	String		m_default_group;
	bool		m_battle_server;
	ScriptFile    * m_scriptFile;
	uint32		m_currentUpdateTime;
	
	std::map<uint8,uint32> m_level_exps;

	CQueueThreadPool<uint32>     m_CharProcessPool;

       /// @copydoc ResourceManager::createImpl
        Resource* createImpl(const String& name, ResourceHandle handle,
            const String& group, bool isManual, ManualResourceLoader* loader,
            const NameValuePairList* createParams);
};


//自动处理Char自动信息，将需要处理的Char放入请求队列中.
class CharacterThread : public ThreadBase
{
        CharManager  * rm;
public:
        CharacterThread(CharManager * s) :ThreadBase("CharThread"),rm(s) {}
        bool run()
        {
                Log.Success("CharacterThread","Characters Process thread start.");
                return rm->StartCharacterProcess();
        }
};


#define   sCharMgr  	 CharManager::getSingleton()



#endif
